{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "56021e7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from neo4j import GraphDatabase\n",
    "import pandas as pd\n",
    "\n",
    "host = 'bolt://localhost:7687'\n",
    "user = 'neo4j'\n",
    "password = 'letmein'\n",
    "driver = GraphDatabase.driver(host,auth=(user, password))\n",
    "\n",
    "def neo4j_query(query, params=None):\n",
    "    with driver.session() as session:\n",
    "        result = session.run(query, params)\n",
    "        return pd.DataFrame([r.values() for r in result], columns=result.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "056df40d",
   "metadata": {},
   "outputs": [],
   "source": [
    "neo4j_query(\"\"\"\n",
    "CREATE CONSTRAINT IF NOT EXISTS ON (n: `NFT`) ASSERT n.`ID_token` IS UNIQUE;\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f84e2cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "// UNWIND $records AS record\n",
    "MERGE (n: `NFT` { `ID_token`: record.`Unique_id_collection` })\n",
    "SET n.`Smart_contract` = record.`Smart_contract`\n",
    "SET n.`Image_url_1` = record.`Image_url_1`\n",
    "SET n.`Image_url_2` = record.`Image_url_2`\n",
    "SET n.`Image_url_3` = record.`Image_url_3`\n",
    "SET n.`Image_url_4` = record.`Image_url_4`\n",
    "SET n.`Name` = record.`Name`\n",
    "SET n.`Description` = record.`Description`\n",
    "//SET n.`Market` = record.`Market`\n",
    "SET n.`Permanent_link` = record.`Permanent_link`;\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "25c48ec5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "CREATE CONSTRAINT IF NOT EXISTS ON (n: `Trader`) ASSERT n.`address` IS UNIQUE;\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fd6d550a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "// UNWIND $records AS record\n",
    "MERGE (n: `Trader` { `address`: record.`Buyer_address` })\n",
    "SET n.`username` = record.`Buyer_username`;\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7966b1b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "// UNWIND $records AS record\n",
    "MERGE (n: `Trader` { `address`: record.`Seller_address` })\n",
    "SET n.`username` = record.`Seller_username`;\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "24b21ab9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "CREATE INDEX ON :Transaction(Transaction_hash);\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "19534513",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "MATCH (seller: `Trader` { `address`: record.`Seller_address` })\n",
    "MATCH (buyer: `Trader` { `address`: record.`Buyer_address` })\n",
    "MATCH (nft: `NFT` { `ID_token`: record.`Unique_id_collection` })\n",
    "\n",
    "CREATE (n: `Transaction` { `Transaction_hash`: record.`Transaction_hash` })\n",
    "SET n.`Price_Crypto` = toFloat(record.`Price_Crypto`)\n",
    "SET n.`Crypto` = record.`Crypto`\n",
    "SET n.`Price_USD` = toFloat(record.`Price_USD`)\n",
    "SET n.`Datetime_updated` = datetime(replace(record.`Datetime_updated`,' ','T'))\n",
    "SET n.`Datetime_updated_seconds` = datetime(replace(record.`Datetime_updated_seconds`,' ', 'T'))\n",
    "SET n.`Market` = record.`Market`\n",
    "MERGE (seller)-[:SOLD]->(n)\n",
    "MERGE (buyer)-[:BOUGHT]->(n)\n",
    "MERGE (n)-[:FOR_NFT]->(nft)\n",
    ";\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0e3a52e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "CREATE CONSTRAINT IF NOT EXISTS ON (n: `Collection`) ASSERT n.`Collection` IS UNIQUE;\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e30c90f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "MERGE (n: `Collection` { `Collection`: record.`Collection_cleaned` })\n",
    ";\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cf24c5d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "CREATE CONSTRAINT IF NOT EXISTS ON (n: `Category`) ASSERT n.`Category` IS UNIQUE;\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "46f6da96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "MERGE (n: `Category` { `Category`: record.`Category` });\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ae4fc28e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "MATCH (source: `NFT` { `ID_token`: record.`Unique_id_collection` })\n",
    "MATCH (target: `Collection` { `Collection`: record.`Collection_cleaned` })\n",
    "MERGE (source)-[r: `IN_COLLECTION`]->(target);\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "afcecf05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "USING PERIODIC COMMIT 50000\n",
    "LOAD CSV WITH HEADERS FROM \"file:///Data_API.csv\" AS record\n",
    "WITH apoc.map.clean(record,[],[\"\",\" \",\"-\"]) AS record\n",
    "MATCH (source: `NFT` { `ID_token`: record.`ID_token` })\n",
    "MATCH (target: `Category` { `Category`: record.`Category` })\n",
    "MERGE (source)-[r: `IN_CATEGORY`]->(target);\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6ff41f0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neo4j_query(\"\"\"\n",
    "CREATE INDEX ON :Trader(username);\n",
    "\"\"\")\n",
    "\n",
    "neo4j_query(\"\"\"\n",
    "CREATE INDEX ON :Transaction(Price_USD);\n",
    "\"\"\")\n",
    "\n",
    "neo4j_query(\"\"\"\n",
    "CREATE INDEX ON :Transaction(Datetime_updated_seconds);\n",
    "\"\"\")\n",
    "\n",
    "neo4j_query(\"\"\"\n",
    "CREATE INDEX ON :NFT(Name);\n",
    "\"\"\")\n",
    "\n",
    "neo4j_query(\"\"\"\n",
    "CREATE INDEX ON :NFT(Description);\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "beeb30e2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
